package com.winway.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winway.boot.mapper.SysUserMapper;
import com.winway.boot.model.entity.SysUser;
import com.winway.boot.service.SysMenuService;
import com.winway.boot.service.SysRoleService;
import com.winway.boot.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统用户服务实现类
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysMenuService sysMenuService;

    @Override
    public SysUser findByUsername(String username) {
        SysUser sysUser = baseMapper.selectOne(new QueryWrapper<SysUser>().eq("user_name", username));
        //获取角色
        sysUser.setRoles(sysRoleService.getRolesByUserId(sysUser.getUserId()));
        //获取权限
        sysUser.setPermissions(sysMenuService.getMenusByUserId(sysUser.getUserId()));
        return sysUser;
    }

    @Override
    public SysUser createUser(SysUser user) {
        user.setDelFlag(0);
        user.setStatus(0);
        baseMapper.insert(user);
        return user;
    }
}
